local daoxing = fk.CreateSkill{
  name = "lingling__daoxing",
}

Fk:loadTranslationTable{
  ["lingling__daoxing"] = "倒行",
  [":lingling__daoxing"] = "回合开始时，你可以视为使用你上回合使用的最后一张即时牌。",

  ["#lingling__daoxing-use"] = "倒行：你可以视为使用【%arg】",
}

daoxing:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(daoxing.name) then
      local turn_event = player.room.logic:getEventsByRule(GameEvent.Turn, 1, function (e)
        return e.data ~= data and e.data.who == player
      end, nil, Player.HistoryGame)
      if #turn_event > 0 then
        local name = ""
        turn_event[1]:searchEvents(GameEvent.UseCard, 1, function (e)
          local use = e.data
          if use.from == player and (use.card.type == Card.TypeBasic or use.card:isCommonTrick()) then
            name = use.card.name
          end
        end)
        if name ~= "" then
          event:setCostData(self, {choice = name})
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local name = event:getCostData(self).choice
    local use = room:askToUseVirtualCard(player, {
      name = name,
      skill_name = daoxing.name,
      prompt = "#lingling__daoxing-use:::"..name,
      cancelable = true,
      extra_data = {
        bypass_times = true,
        extraUse = true,
      },
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self).extra_data)
  end,
})

return daoxing
